package com.example.messengerapp.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.messengerapp.databinding.FragmentChatListBinding
import com.example.messengerapp.viewmodel.ChatListViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

class ChatListFragment : Fragment() {

    private lateinit var binding: FragmentChatListBinding
    private val viewModel: ChatListViewModel by viewModel()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentChatListBinding.inflate(inflater, container, false)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel

        setupRecyclerView()

        return binding.root
    }

    private fun setupRecyclerView() {
        val adapter = ChatListAdapter(emptyList()) { chat ->
            // Handle chat item click
        }
        binding.recyclerViewChats.layoutManager = LinearLayoutManager(context)
        binding.recyclerViewChats.adapter = adapter

        viewModel.chats.observe(viewLifecycleOwner, { chats ->
            (binding.recyclerViewChats.adapter as ChatListAdapter).apply {
                this.chats = chats
                notifyDataSetChanged()
            }
        })
    }
}
